package com.example.baseadapter;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	// Database Version
	private static final int DATABASE_VERSION = 10;
	// Database Name
	private static final String DATABASE_NAME = "legalDiary";
	// Table Name
	private static final String TABLE_CASE = "cases";
	private static final String TABLE_DATE = "dates";
	// Column names
	private static final String KEY_CASE_ID = "caseId";
	private static final String KEY_PLAINTIFF = "plaintiff";
	private static final String KEY_RESPONDENT = "respondent";
	private static final String KEY_CASE_REF_NO = "caseRefNo";
	private static final String KEY_CASE_NOTES = "caseNotes";
	private static final String KEY_COURT_DETAILS = "courtDetails";
	private static final String KEY_DATE_ID = "dateId";
	private static final String KEY_DAY = "day";
	private static final String KEY_YEAR = "year";
	private static final String KEY_MONTH = "month";
	private static final String KEY_DATE_NOTES = "dateNote";

	private static final String CREATE_TABLE_CASE = "CREATE TABLE "
			+ TABLE_CASE + "(" + KEY_CASE_ID + " INTEGER PRIMARY KEY,"
			+ KEY_PLAINTIFF + " TEXT," + KEY_RESPONDENT + " TEXT,"
			+ KEY_CASE_REF_NO + " TEXT," + KEY_CASE_NOTES + " TEXT,"
			+ KEY_COURT_DETAILS + " TEXT" + ")";

	private static final String CREATE_TABLE_DATE = "CREATE TABLE "
			+ TABLE_DATE + "(" + KEY_DATE_ID + " INTEGER PRIMARY KEY,"
			+ KEY_DAY + " INTEGER," + KEY_MONTH + " INTEGER,"
			+ KEY_YEAR + " INTEGER," + KEY_CASE_ID + " INTEGER,"
			+ KEY_DATE_NOTES + " TEXT" + ")";

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		// creating required tables
		db.execSQL(CREATE_TABLE_CASE);
		db.execSQL(CREATE_TABLE_DATE);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// on upgrade drop older tables
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CASE);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATE);

		// create new tables
		onCreate(db);
	}
    //creating a case row in table 
	public long createCase(Case caseObj) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(KEY_PLAINTIFF, caseObj.getPlaintiff());
		values.put(KEY_RESPONDENT, caseObj.getRespondent());
		values.put(KEY_CASE_REF_NO, caseObj.getCaseRefno());
		values.put(KEY_CASE_NOTES, caseObj.getCaseDetail());
		values.put(KEY_COURT_DETAILS, caseObj.getCourtDetail());
		return db.insert(TABLE_CASE, null, values);

	}
//getting number of rows or cases in case table
	public int getCaseCount() {
		String countQuery = "SELECT  * FROM " + TABLE_CASE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);

		int count = cursor.getCount();
		cursor.close();

		// return count
		return count;
	}
//get data of all cases inside case table.
	public List<Case> getAllCases() {
		List<Case> caseList = new ArrayList<Case>();
		String selectQuery = "SELECT  * FROM " + TABLE_CASE;

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor c = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (c.moveToFirst()) {
			do {
				Case caseObject = new Case();
				caseObject.setCaseId(c.getInt((c.getColumnIndex(KEY_CASE_ID))));
				caseObject.setPlaintiff((c.getString(c
						.getColumnIndex(KEY_PLAINTIFF))));
				caseObject.setRespondent(c.getString(c
						.getColumnIndex(KEY_RESPONDENT)));
				caseObject.setCaseRefno(c.getString(c
						.getColumnIndex(KEY_CASE_REF_NO)));
				caseObject.setCaseDetail(c.getString(c
						.getColumnIndex(KEY_CASE_NOTES)));
				caseObject.setCourtDetail(c.getString(c
						.getColumnIndex(KEY_COURT_DETAILS)));

				// adding to todo list
				caseList.add(caseObject);
			} while (c.moveToNext());
		}

		return caseList;
	}

	
	//get a single row data or single case data by giving case id
	 public Case getSingleCase(long case_id) {
	        SQLiteDatabase db = this.getReadableDatabase();
	 
	        String selectQuery = "SELECT  * FROM " + TABLE_CASE + " WHERE "
	                + KEY_CASE_ID + " = " + case_id;
	 
	        Cursor c = db.rawQuery(selectQuery, null);
	 
	        if (c != null)
	            c.moveToFirst();
	 
	        Case td = new Case();
	        td.setCaseId(c.getInt(c.getColumnIndex(KEY_CASE_ID)));
	        td.setPlaintiff((c.getString(c.getColumnIndex(KEY_PLAINTIFF))));
	        td.setRespondent(c.getString(c.getColumnIndex(KEY_RESPONDENT)));
	        td.setCaseRefno(c.getString(c.getColumnIndex(KEY_CASE_REF_NO)));
	        td.setCaseDetail(c.getString(c.getColumnIndex(KEY_CASE_NOTES)));
	        td.setCourtDetail(c.getString(c.getColumnIndex(KEY_COURT_DETAILS)));
	        return td;
	    }
	 
	 //updating case data.
	 public int updateCase(Case caseObj) {
	        SQLiteDatabase db = this.getWritableDatabase();
	 
	        ContentValues values = new ContentValues();
	        values.put(KEY_PLAINTIFF, caseObj.getPlaintiff());
			values.put(KEY_RESPONDENT, caseObj.getRespondent());
			values.put(KEY_CASE_REF_NO, caseObj.getCaseRefno());
			values.put(KEY_CASE_NOTES, caseObj.getCaseDetail());
			values.put(KEY_COURT_DETAILS, caseObj.getCourtDetail());
	 
	        // updating row
	        return db.update(TABLE_CASE, values, KEY_CASE_ID + " = ?",
	                new String[] { String.valueOf(caseObj.getCaseId()) });
	    }
	 
	 //delete case data
	 
	 public void deleteCaseHelper(long caseId) {
		    SQLiteDatabase db = this.getWritableDatabase();
		    db.delete(TABLE_CASE, KEY_CASE_ID + " = ?",
		            new String[] { String.valueOf(caseId) });
		    
		    //Toast.makeText(null, "Case Deleted Succuessfully",Toast.LENGTH_LONG).show();
		}
	 
	 //creating table date data.
	public long createCaseDate(int id, Date d, String dateNote) {
		int day = d.getDay();
		int month = d.getMonth();
		int year = d.getYear();
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues values = new ContentValues();

		values.put(KEY_DAY, day);
		values.put(KEY_MONTH, month);
		values.put(KEY_YEAR, year);
		values.put(KEY_CASE_ID, id);
		values.put(KEY_DATE_NOTES, dateNote);
		return db.insert(TABLE_DATE, null, values);
	}
	
//getting number of rows inside date table
	public int getDateCount() {
		String countQuery = "SELECT  * FROM " + TABLE_DATE;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		int count = cursor.getCount();
		cursor.close();
		// return count
		return count;

	}
}